/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controllers.managecontrollers;

import classes.Breadcrumbs;
import classes.HibernateUtil;
import dao.CategoryDao;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import models.Category;

/**
 *
 * @author Vuong Pham<Vuong.Pham@hva.nl>
 */
public class ManageCategoryController extends HttpServlet {

    /**
     * This do post method adds or edits a category in the database.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //----------------------------------------------------------------
        //Get inputs from form
        //----------------------------------------------------------------
        String name = request.getParameter("name");
        String description = request.getParameter("description");

        Map<String, String> errors = new HashMap<String, String>();

        if (name.matches(".*\\d.*")) {
            errors.put("name", "Name cannot contain digits");
        }

        if (request.getParameter("formoption").equals("categoryedit")) { //edits.

            long id = Long.parseLong(request.getParameter("categoryid"));

            HibernateUtil hu = new HibernateUtil();
            CategoryDao ed = new CategoryDao(hu.openSession());

            Category cattemp = ed.get(id);
            cattemp.setName(name);
            cattemp.getDescription();

            ed.update(cattemp);

        } else { //add

            // Handling the errors...
            if (!errors.isEmpty()) {
                request.setAttribute("error", errors);
                request.getRequestDispatcher("jsp/add_edit_categories.jsp").forward(request, response);
                return;
            }

            //----------------------------------------------------------------
            //Create user and employee object and save them
            //----------------------------------------------------------------
            HibernateUtil hu = new HibernateUtil();
            long category_id = System.nanoTime();
            Category category = new Category(category_id, name, description);

            CategoryDao ud = new CategoryDao(hu.openSession());
            ud.save(category);

        }
        response.sendRedirect("Category?edit");
    }
    /*
     * this do get methods adds the breadcrumbs and redirects to the add edit page.
     */

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // Breadcrumbs
        Breadcrumbs[] breadcrumbs = {
            new Breadcrumbs("Index", "Home"),
            new Breadcrumbs("Category?edit", "Manage categories and courses"),
            new Breadcrumbs("ManageCategory", "Add category")
        };
        request.setAttribute("breadcrumbs", breadcrumbs);
        request.getRequestDispatcher("jsp/add_edit_categories.jsp").forward(request, response);
    }
}